package com.example.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.util.Random;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import com.example.fw.GroupObject;
import com.example.fw.Groups;

public class GroupModificationTests extends TestBase {
	
	Random rnd = new Random();
	
	@Test
	public void testSomeGroupCanBeModificated() throws Exception {
		Groups oldGroupList = app.getGroupHelper().getGroups();
		GroupObject groupBeforeModification = oldGroupList.getSomeGroupFromList();
		GroupObject groupAfterModification = new GroupObject(groupBeforeModification);
		groupAfterModification.setName("name-edited"+rnd.nextInt())
								.setHeader("header-edited"+rnd.nextInt())
								.setFooter("footer-edited"+rnd.nextInt());
		//GroupObject groupBeforeModification = oldGroupList.iterator().next();
		app.getGroupHelper().modifyGroup(groupBeforeModification, groupAfterModification);
		Groups newGroupList = app.getGroupHelper().getGroups();
		assertThat(newGroupList.withAdded(groupBeforeModification), equalTo(oldGroupList.withAdded(groupAfterModification)));		//	hamcrest style lesson 4 65:00
		//verifyGroupModificated(oldGroupList, groupBeforeModification, groupAfterModification, newGroupList);
	}
	
	@AfterMethod		// lesson 7 ~ 75:00
	public void testUiAndDbConformsWithModel()	{
		if (app.getProperty("ui.check") != null)	{
			Groups modelList = app.getGroupHelper().getGroups();
			Groups uiList = app.getGroupHelper().getUiGroups();
			assertThat(uiList, equalTo(modelList));
		}
		if (app.getProperty("db.check") != null)	{
			Groups modelList = app.getGroupHelper().getGroups();
			Groups dbList = app.getHibernateHelper().getGroups();
			assertThat(dbList, equalTo(modelList));
		}
	}

//	public GroupObject takeSomeGroupFromList(Groups groupList) {
//		//Groups groups = new Groups(storedGroups);
//		GroupObject someGroupFromList = groupList.storedGroups.iterator().next();
//		return someGroupFromList;
//	}

//	protected void verifyGroupModificated(Set<GroupObject> oldGroupList, GroupObject groupBeforeModification, GroupObject groupAfterModification, Set<GroupObject> newGroupList) {
//		assertThat(newGroupList.size(), equalTo(oldGroupList.size()));
//		oldGroupList.remove(groupBeforeModification);
//		groupAfterModification.setId("$$$");
//		oldGroupList.add(groupAfterModification);
//		groupAfterModification.setId(null);
//		assertThat(newGroupList, equalTo(oldGroupList));
//	}
}
